﻿/*
 * RM2 Character Generator
 * Copyright (C) 2009 James Culshaw
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of 
 * the GNU General Public License as published by the Free Software Foundation; either version 2 
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; 
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace RMCharGen.Data
{
    internal static class Loader
    {
        private const string SkillLoadError = "A problem arose while loading the skills data";

        private const string SkillsFileName = "Skills.xml";

        private const string NameAttribute = "name";
        private const string StatOneAttribute = "statOne";
        private const string StatTwoAttribute = "statTwo";
        private const string StatThreeAttribute = "statThree";
        private const string TypeOneAttribute = "typeOne";
        private const string TypeTwoAttribute = "typeTwo";
        private const string PrimaryAttribute = "primary";

        private static Skill LoadSkill(XmlElement skillElement)
        {
            Skill s = new Skill();

            s.Name = skillElement.GetAttribute(NameAttribute).Trim();
            if (skillElement.HasAttribute(StatOneAttribute))
            {
                s.StatOne = skillElement.GetAttribute(StatOneAttribute).Trim();
            }
            if (skillElement.HasAttribute(StatTwoAttribute))
            {
                s.StatTwo = skillElement.GetAttribute(StatTwoAttribute).Trim();
            }
            if (skillElement.HasAttribute(StatThreeAttribute))
            {
                s.StatThree = skillElement.GetAttribute(StatThreeAttribute).Trim();
            }
            if (skillElement.HasAttribute(TypeOneAttribute))
            {
                s.TypeOne = skillElement.GetAttribute(TypeOneAttribute).Trim();
            }
            if (skillElement.HasAttribute(TypeTwoAttribute))
            {
                s.TypeTwo = skillElement.GetAttribute(TypeTwoAttribute).Trim();
            }
            s.IsPrimary = skillElement.HasAttribute(PrimaryAttribute);
    
            return s;
        }

        internal static Dictionary<string, Skill> LoadSkills()
        {
            Dictionary<string, Skill> skills = new Dictionary<string, Skill>();

            XmlDocument skillsDom = new XmlDocument();
            Skill newSkill;

            try
            {
                skillsDom.Load(SkillsFileName);
                foreach (XmlNode skill in skillsDom.DocumentElement.ChildNodes)
                {
                    newSkill =  LoadSkill((XmlElement)skill);
                    skills.Add(newSkill.Name, 
                               newSkill);
                }
            }
            catch(Exception ex)
            {
                throw new DataLoadException(SkillLoadError,
                                            ex);
            }
            return skills;
        }
    }
}
